//
//  FWFooterView.m
//  BirdLoveSheep
//
//  Created by wwj on 16/4/12.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWMeFooterView.h"
#import "FWMeService.h"
#import "FWSqaureButton.h"

@interface FWMeFooterView ()
@property (strong, nonatomic) NSArray *dataArray;
@end
@implementation FWMeFooterView

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

+ (instancetype)footerView{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        FWMeRequest *request = [[FWMeRequest alloc] init];
        request.a = @"square";
        request.c = @"topic";
        [FWMeService getMeFooterDataWith:request success:^(FWMeResponse *response) {
            self.dataArray = response.square_list;
            [self initView];
        } failure:nil];
    }
    return self;
}


- (void)initView{
    NSInteger maxCount = 4;
    CGFloat btnW = kScreenW / maxCount;
    CGFloat btnH = btnW;
    for (NSInteger index = 0; index < self.dataArray.count; index++) {
        NSInteger col = index % maxCount;
        NSInteger row = index / maxCount;
        FWSqaureButton *btn = [FWSqaureButton buttonWithType:UIButtonTypeCustom];
        btn.model = self.dataArray[index];
        btn.tag = index;
        [btn addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
        
        btn.x = btnW * col;
        btn.y = btnH * row;
        btn.width = btnW;
        btn.height = btnH;
        
    }
    
    NSInteger maxRow = (self.dataArray.count + maxCount - 1) / maxCount;
    self.height = maxRow * btnH + 5;
    self.width = kScreenW;
    if (_heightBlock) {
        _heightBlock(self.height);
    }
}

- (void)setFrame:(CGRect)frame{
    frame.origin.x = 0;
    [super setFrame:frame];
}

- (void)onClickBtn:(FWSqaureButton *)btn{
    if (_block) {
        self.block(self.dataArray[btn.tag]);
    }
}

@end
